jQuery(function() {
    var i = 1;
    jQuery('#form-item-wrapper textarea').keyup(function(){
        var length = jQuery(this).val().length;
        jQuery(this).parent().find('.ghi-chu-length').text(length);
    });
    jQuery('#form-item-wrapper textarea').keyup();
    jQuery('#add-more-vat-tu').click(function(){
        i++;
        var html = '<div class="form-item-row">' +
                        '<div class="form-item form-text-wrapper">' +
                            '<label for="txt-ten-vat-tu-'+i+'">Ten vat tu</label>' +
                            '<input id="txt-ten-vat-tu-'+i+'" name="ten_vat_tu[]" type="text" class="form-text form-autocomplete text" autocomplete="OFF" />' +
                            '<input class="autocomplete" type="hidden" id="txt-ten-vat-tu-'+i+'-autocomplete" value="/drupal-7.9/vat_tu/autocomplete" disabled="disabled" />' +
                        '</div>' +
                        '<div class="form-item form-text-wrapper">' +
                            '<label for="txt-so-luong-'+i+'">So luong</label>' +
                            '<input id="txt-so-luong-'+i+'" name="so_luong[]" type="text" class="form-text text" />' +
                        '</div>' +
                        '<div class="form-item form-textarea-wrapper">' +
                            '<label for="ghi-chu-'+i+'">Ghi chú</label>' +
                            '<textarea id="ghi-chu-'+i+'" name="ghi_chu[]" class="form-textarea" cols="60" rows="5"></textarea>' +
                            '<div class="description">Nhập tối đa 200 ký tự. Số lượng hiện thời: <span style="color: red;" class="ghi-chu-length">0</span></div>' +
                        '</div>' +
                      '</div>';
        jQuery('#form-item-wrapper').append(html);
        var input = jQuery('#txt-ten-vat-tu-' + i)
            .attr('autocomplete', 'OFF')
            .attr('aria-autocomplete', 'list');
        jQuery(input[0].form).submit(Drupal.autocompleteSubmit);
        input.parent()
            .attr('role', 'application')
            .append(jQuery('<span class="element-invisible" aria-live="assertive"></span>')
              .attr('id', input.attr('id') + '-autocomplete-aria-live')
            );
        var auto_path = new Drupal.ACDB(jQuery('#txt-ten-vat-tu-1-autocomplete').val());
        new Drupal.jsAC(input, auto_path);
        jQuery('#ghi-chu-'+i).keyup(function(){
            var length = jQuery(this).val().length;
            jQuery(this).parent().find('.ghi-chu-length').text(length);
        });
    });
});